package com.imyours.model.util;

import java.util.List;

import org.hibernate.classic.Session;

import com.imyours.model.department.DepartmentEntity;
import com.imyours.model.user.UserEntity;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

@Service(name="init")
public class InitUtil extends ContextService  {

	public InitUtil(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void execute() {
		Session se = HibernateUtil.getSessionFactory().openSession();
		try{
		List<UserEntity> countRe =  se.createQuery("from UserEntity ").setMaxResults(1).list();
		}catch(Exception ex){
			se.close();
			se = HibernateUtil.getInitDatabaseSessionFactory().openSession();
			
			UserEntity adminUser = new UserEntity();
			adminUser.setUsername("admin");
			adminUser.setPassword("password");
			adminUser.setNickname("admin");
			DepartmentEntity rootDepartment = new DepartmentEntity();
			rootDepartment.setName("imyours hello world");
			rootDepartment.setId(-1);
			
			
			se.beginTransaction();
			se.save(adminUser);
			se.save(rootDepartment);
			se.getTransaction().commit();
			se.close();
		}
	}

	
	
	
}
